<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/29
 * Time: 18:04
 */

namespace App\HttpController\Home;


use App\Utility\Curl;
use EasySwoole\Http\AbstractInterface\Controller;

class Base extends Controller
{
    public function index()
    {
        // TODO: Implement index() method.
    }

    public function jsonToArr($data)
    {

        foreach ( $data as $k=>$v ){
            if( is_string($v) && $this->analyJson($v) ){
                $data[$k] = json_decode($v,true);
            }
        }
        return $data;
    }
    function analyJson($json_str) {
        $json_str = str_replace('＼＼', '', $json_str);
        $out_arr = array();
        preg_match('/{.*}/', $json_str, $out_arr);
        if (!empty($out_arr)) {
            $result = json_decode($out_arr[0], TRUE);
        } else {
            return FALSE;
        }
        return true;
    }
    function handleCurl($method,$url,$params = null)
    {

        $request = new Curl();
        $response = $request->request($method,$url,$params);
        $content = $response->getBody();

        return json_decode($content, true);
    }
    //毫秒
    function getMillisecond() {
        list($s1, $s2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }
    public function getMsecToMescdate($msectime)
    {
        $msectime = $msectime * 0.001;
        if(strstr($msectime,'.')){
            sprintf("%01.3f",$msectime);
            list($usec, $sec) = explode(".",$msectime);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT);
        }else{
            $usec = $msectime;
            $sec = "000";
        }
        $date = date("Y-m-d H:i:s",$usec);
        return $mescdate = str_replace('x', $sec, $date);
    }
}